/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.code.restui.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Rohit
 */
public class HttpResponse {

    private int responseCode;
    private String responseMsg;
    private String contentType;
    private String contentEncoding;
    private long lastModified;
    private boolean hasError;
    private String errorMsg;
    private Map<String, List<String>> headers;
    private String data;

    private boolean requireLogin;
    private String authType;
    private String authRealm;
    

    public HttpResponse() {
    }

    public HttpResponse(boolean requireLogin, String authType, String authRealm) {
        this.requireLogin = requireLogin;
        this.authType = authType;
        this.authRealm = authRealm;
    }



    public HttpResponse(int responseCode, String responseMsg, String contentType, String contentEncoding, long lastModified, Map<String, List<String>> headers, String data) {
        this.responseCode = responseCode;
        this.responseMsg = responseMsg;
        this.contentType = contentType;
        this.contentEncoding = contentEncoding;
        this.lastModified = lastModified;
        this.headers = headers;
        this.data = data;
        this.hasError = false;
        this.requireLogin = false;
    }

    public HttpResponse(boolean hasError, String errorMsg) {
        this.hasError = hasError;
        this.errorMsg = errorMsg;
    }

    public HttpResponse(HttpURLConnection huc) {
        try {
            this.responseCode = huc.getResponseCode();
            this.responseMsg = huc.getResponseMessage();
            this.contentType = huc.getContentType();
            this.contentEncoding = huc.getContentEncoding();
            this.lastModified = huc.getLastModified();
            this.headers = huc.getHeaderFields();

            BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream()));

            String inputLine;
            while ((inputLine = br.readLine()) == null) {
                this.data += inputLine;
            }


        } catch (IOException ex) {
            Logger.getLogger(HttpResponse.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public long getLastModified() {
        return lastModified;
    }

    public void setLastModified(long lastModified) {
        this.lastModified = lastModified;
    }

    public String getContentEncoding() {
        return contentEncoding;
    }

    public void setContentEncoding(String contentEncoding) {
        this.contentEncoding = contentEncoding;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public Map<String, List<String>> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, List<String>> headers) {
        this.headers = headers;
    }

    public String getHeader(String headerName) {
        List<String> values = this.headers.get(headerName);
        if (values == null) {
            return null;
        } else {
            String value = "";
            for (String val : values) {
                value += val + ",";
            }
            return value.substring(0, value.length() - 1);
        }

    }

    public int getResponseCode() {
        return responseCode;
    }

    public void setResponseCode(int responseCode) {
        this.responseCode = responseCode;
    }

    public String getResponseMsg() {
        return responseMsg;
    }

    public void setResponseMsg(String responseMsg) {
        this.responseMsg = responseMsg;
    }

    public String getAuthRealm() {
        return authRealm;
    }

    public void setAuthRealm(String authRealm) {
        this.authRealm = authRealm;
    }

    public String getAuthType() {
        return authType;
    }

    public void setAuthType(String authType) {
        this.authType = authType;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public boolean isHasError() {
        return hasError;
    }

    public void setHasError(boolean hasError) {
        this.hasError = hasError;
    }

    public boolean isRequireLogin() {
        return requireLogin;
    }

    public void setRequireLogin(boolean requireLogin) {
        this.requireLogin = requireLogin;
    }

    
}
